package com.xinghou.platform.dao.main.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.Sphere;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.mongodb.WriteResult;
import com.xinghou.platform.dao.main.StoreLocationMONGO;
import com.xinghou.platform.model.main.StoreLocationGeo;

/**
 * 
 * @author xuzhen
 *
 */
@Service
public class StoreLocationMONGOImpl {/*

	@Resource
	private MongoTemplate mongoTemplate;
	
	*//**
	 * 创建mongo数据表
	 *//*
	public void createCollection() {  
        if (!this.mongoTemplate.collectionExists(StoreLocationGeo.class)) {  
            this.mongoTemplate.createCollection(StoreLocationGeo.class);  
        }
    }

	
	*//*public void insert(StoreLocationGeo storeLocationGeo) {
		mongoTemplate.insert(storeLocationGeo);
		
	}*//*

	
	*//*public StoreLocationGeo findOne(int id) {
		return mongoTemplate.findOne(new Query(Criteria.where("_id").is(id)), StoreLocationGeo.class);
	}*//*

	
	*//*public StoreLocationGeo findOnebyStoreid(int storeid) {
		return mongoTemplate.findOne(new Query(Criteria.where("storeid").is(storeid)), StoreLocationGeo.class);
	}*//*

	
	*//*public List<StoreLocationGeo> findAll() {
		return mongoTemplate.findAll(StoreLocationGeo.class);
	}*//*

	
	public List<StoreLocationGeo> nearSphere(Point point,int limit,int skip,double maxDistance) {
		Criteria criteria = Criteria.where("pos").nearSphere(point).maxDistance(maxDistance);

		Query query = new Query(criteria);
		
		return mongoTemplate.find(query.limit(limit).skip(skip), StoreLocationGeo.class);
	}

	
	*//*public List<StoreLocationGeo> near(Point point, String orderFeild,
			int limit, double maxDistance) {
		Criteria criteria = Criteria.where("pos").near(point).maxDistance(maxDistance);

		Query query = new Query(criteria);
		if(orderFeild != null){
			query.with(new Sort(new Sort.Order(Direction.DESC, orderFeild)));
		}
		return mongoTemplate.find(query.limit(limit), StoreLocationGeo.class);
	}
*//*
	
	*//*public List<StoreLocationGeo> withinSphere(Point center, String orderFeild,
			int limit, double radius) {
		Circle circle = new Circle(center, radius);
		Criteria criteria = Criteria.where("pos").withinSphere(circle);
		Query query = new Query(criteria);
		if(orderFeild != null)
			query.with(new Sort(new Sort.Order(Direction.DESC, orderFeild)));
		return mongoTemplate.find(query.limit(limit), StoreLocationGeo.class);
	}*//*

	
	*//*public List<StoreLocationGeo> within(Point center, String orderFeild,
			int limit, double radius) {
		Sphere circle = new Sphere(center, radius);
		Criteria criteria = Criteria.where("pos").within(circle);
		Query query = new Query(criteria);
		if(orderFeild != null)
			query.with(new Sort(new Sort.Order(Direction.DESC, orderFeild)));
		return mongoTemplate.find(query.limit(limit), StoreLocationGeo.class);
	}
*//*
	
	public boolean update(StoreLocationGeo storeLocationGeo) {
		boolean result = false;
		
		Query query = new Query();  
        query.addCriteria(new Criteria("storeid").is(storeLocationGeo.getStoreid()));  
        Update update = new Update();  
        update.set("pos", storeLocationGeo.getPos());
        update.set("createtime", storeLocationGeo.getCreatetime());
        WriteResult writeResult = this.mongoTemplate.updateFirst(query, update, StoreLocationGeo.class);  
        //this.mongoTemplate.updateFirst(query, update, StoreLocationGeo.class);
		if (!writeResult.wasAcknowledged()) {
            result = true;
        }
		
		return result;
	}  */


}
